package com.frikinet.lifeguard.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.apache.commons.lang.StringUtils;

import com.frikinet.lifeguard.App;
import com.frikinet.lifeguard.events.MonthSelectedListener;
import com.frikinet.lifeguard.events.PostSelectedEvent;
import com.frikinet.lifeguard.events.PostSelectedListener;
import com.frikinet.lifeguard.events.UserSelectedEvent;
import com.frikinet.lifeguard.events.UserSelectedListener;
import com.frikinet.lifeguard.events.YearMonthSelectedEvent;
import com.frikinet.lifeguard.events.YearSelectedListener;
import com.frikinet.lifeguard.model.Post;
import com.frikinet.lifeguard.model.User;

public class BreadCrumbs extends JPanel implements UserSelectedListener, YearSelectedListener, MonthSelectedListener, PostSelectedListener
{
	private static final long serialVersionUID = 1L;
	
	private Font clickFont, normalFont;
	
	private List<BreadCrumb> crumbs = new ArrayList<BreadCrumb>();
	
	private static class BreadCrumb extends MouseAdapter
	{
		private String text;
		private Object event;
		public BreadCrumb(String text, Object event)
		{
			this.text = text;
			this.event = event;
		}
		public String getText()
		{
			return text;
		}
		@Override
		public void mouseReleased(MouseEvent e)
		{
			if(event instanceof UserSelectedEvent)
			{
				App.events.fireUserSelected((UserSelectedEvent)event);
			}
			else if(event instanceof PostSelectedEvent)
			{
				App.events.firePostSelected((PostSelectedEvent)event);
			}
			else if(event instanceof YearMonthSelectedEvent)
			{
				YearMonthSelectedEvent yme = (YearMonthSelectedEvent)event;
				if(yme.getMonth()==0)
					App.events.fireYearSelected(yme);
				else
					App.events.fireMonthSelected(yme);
			}
		}
	}

	public BreadCrumbs()
	{
		super(new FlowLayout(FlowLayout.LEFT));
		Font defaultFont = UIManager.getDefaults().getFont("Label.font");
		clickFont = new Font(defaultFont.getName(), Font.BOLD, defaultFont.getSize()-1);
		normalFont = new Font(defaultFont.getName(), defaultFont.getStyle(), defaultFont.getSize()-1);
		JLabel first = new JLabel(" ");
		first.setFont(normalFont);
		add(first);
		setBorder(BorderFactory.createRaisedBevelBorder());
		
		App.events.addUserSelectedListener(this);
		App.events.addYearSelectedListener(this);
		App.events.addMonthSelectedListener(this);
		App.events.addPostSelectedListener(this);
	}
	
	
	private void refresh()
	{
		removeAll();
		for(int i=0;i < crumbs.size();i++)
		{
			BreadCrumb crumb = crumbs.get(i);
			boolean last = i == crumbs.size()-1;
			JLabel label = new JLabel(!last ? String.format("%s >", crumb.getText()) : String.format("%s", crumb.getText()));
			label.setFont(last ? normalFont : clickFont);
			if(!last)
			{
				label.setForeground(Color.blue);
				label.addMouseListener(crumb);
				label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			}
			add(label);
		}
		repaint();
	}


	@Override
	public void postSelected(PostSelectedEvent event)
	{
		crumbs.clear();
		Post post = event.getPost();
		User user = post.getUser();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(post.getDate());
		crumbs.add(new BreadCrumb(user.getName(), new UserSelectedEvent(this, user)));
		crumbs.add(new BreadCrumb(App.i18n.translate("comp.crumbs.year", post.getDate()), new YearMonthSelectedEvent(this, user, calendar.get(Calendar.YEAR))));
		crumbs.add(new BreadCrumb(StringUtils.capitalize(App.i18n.translate("comp.crumbs.month", post.getDate())), new YearMonthSelectedEvent(this, user, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1)));
		crumbs.add(new BreadCrumb(StringUtils.capitalize(App.i18n.translate("comp.crumbs.day", post.getDate())), new PostSelectedEvent(this, post)));
		refresh();
	}

	@Override
	public void monthSelected(YearMonthSelectedEvent event)
	{
		crumbs.clear();
		User user = event.getUser();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, event.getYear());
		calendar.set(Calendar.MONTH, event.getMonth()-1);
		crumbs.add(new BreadCrumb(user.getName(), new UserSelectedEvent(this, user)));
		crumbs.add(new BreadCrumb(App.i18n.translate("comp.crumbs.year", calendar.getTime()), new YearMonthSelectedEvent(this, user, calendar.get(Calendar.YEAR))));
		crumbs.add(new BreadCrumb(StringUtils.capitalize(App.i18n.translate("comp.crumbs.month", calendar.getTime())), new YearMonthSelectedEvent(this, user, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1)));
		refresh();
	}

	@Override
	public void yearSelected(YearMonthSelectedEvent event)
	{
		crumbs.clear();
		User user = event.getUser();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, event.getYear());
		crumbs.add(new BreadCrumb(user.getName(), new UserSelectedEvent(this, user)));
		crumbs.add(new BreadCrumb(App.i18n.translate("comp.crumbs.year", calendar.getTime()), new YearMonthSelectedEvent(this, user, calendar.get(Calendar.YEAR))));
		refresh();
	}

	@Override
	public void userSelected(UserSelectedEvent event)
	{
		crumbs.clear();
		crumbs.add(new BreadCrumb(event.getUser().getName(), new UserSelectedEvent(this, event.getUser())));
		refresh();
	}

	
}
